﻿using System.Text;

namespace SdmxMl.Common
{
    /// <summary>Base class for Artefact reference </summary>
    public class ArtefactRefBase : ISdmxArtefactRef
    {
        #region Properties

        public string FullIdent
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(AgencyId).Append('+');
                sb.Append(Id).Append('+');
                sb.Append(Version);
                return sb.ToString();
            }
        }

        #region ISdmxArtefactRef Members

        protected string _agencyId;
        public string AgencyId
        {
            get
            {
                return _agencyId;
            }
            set
            {
                _agencyId = value;
            }
        }

        protected string _id;
        public string Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }

        protected string _version;
        public string Version
        {
            get { return _version; }
            set
            {
                _version = value;
                if (string.IsNullOrEmpty(_version))
                    _version = "1.0";
            }
        }

        #endregion

        public bool IsFilled
        {
            get
            {
                return string.IsNullOrEmpty(Id) == false &&
                    string.IsNullOrEmpty(AgencyId) == false;
            }
        }

        private string _urn;
        public virtual string Urn
        {
            get { return _urn; }
            set { _urn = value; }
        }
        #endregion

        #region Constructors

        public ArtefactRefBase()
        {
            Id = AgencyId = string.Empty;
            Version = "1.0";
            _urn = string.Empty;
        }

        public ArtefactRefBase(string id, string agency, string vers)
        {
            Id = id;
            AgencyId = agency;
            Version = vers;
            _urn = string.Empty;
        }

        public ArtefactRefBase(string fullId)
        {
            if (fullId == null)
                fullId = string.Empty;
            string[] arr = fullId.Split('+');
            if (arr.Length == 3)
            {
                Id = arr[1];
                AgencyId = arr[0];
                Version = arr[2];
            }
            else
            {
                Id = AgencyId = string.Empty;
                Version = "1.0";
                _urn = string.Empty;
            }
        }

        #endregion

        #region Update Reference menbers

        public void UpdateRef(ArtefactRefBase arb)
        {
            if (arb != null)
            {
                AgencyId = arb.AgencyId;
                Id = arb.Id;
                Version = arb.Version;
            }
        }
        #endregion

        #region string representation

        public override string ToString()
        {
            return AgencyId + "+" + Id + "+" + Version;
        }
        #endregion

    }
}
